<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li class="active"><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t0React基础篇">React基础篇</a><ul><li><a href="#t11.什么是React?">1.什么是React?</a></li><li><a href="#t22.组件化的概念">2.组件化的概念</a></li></ul></li><li><a href="#t33.跑通react开发环境">3.跑通react开发环境</a></li><li><a href="#t44.简介JSX">4.简介JSX</a></li><li><a href="#t55.createElement">5.createElement</a></li><li><a href="#t66.react元素/JSX元素">6.react元素/JSX元素</a></li><li><a href="#t77.模拟render实现">7.模拟render实现</a></li><li><a href="#t88.JSX表达式的用法">8.JSX表达式的用法</a></li><li><a href="#t99.JSX属性">9.JSX属性</a></li><li><a href="#t1010.组件的特点声明方式">10.组件的特点声明方式</a></li><li><a href="#t1111.组件的两种定义方式">11.组件的两种定义方式</a></li><li><a href="#t1212.组件中属性和状态的区别">12.组件中属性和状态的区别</a></li><li><a href="#t1313.绑定事件">13.绑定事件</a></li><li><a href="#t1414.属性校验,默认属性">14.属性校验,默认属性</a></li><li><a href="#t1515.状态的使用">15.状态的使用</a></li><li><a href="#t1616.复合组件">16.复合组件</a></li><li><a href="#t1717.受控组件和非受控组件">17.受控组件和非受控组件</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t0React&#x57FA;&#x7840;&#x7BC7;">React&#x57FA;&#x7840;&#x7BC7; <a href="#t0React&#x57FA;&#x7840;&#x7BC7;"> # </a></h2>
<h3 id="t11.&#x4EC0;&#x4E48;&#x662F;React?">1.&#x4EC0;&#x4E48;&#x662F;React? <a href="#t11.&#x4EC0;&#x4E48;&#x662F;React?"> # </a></h3>
<p>React &#x662F;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x6784;&#x5EFA;&#x7528;&#x6237;&#x754C;&#x9762;&#x7684;JavaScript&#x5E93;
&#x6838;&#x5FC3;&#x4E13;&#x6CE8;&#x4E8E;&#x89C6;&#x56FE;,&#x76EE;&#x7684;&#x5B9E;&#x73B0;&#x7EC4;&#x4EF6;&#x5316;&#x5F00;&#x53D1;</p>
<h3 id="t22.&#x7EC4;&#x4EF6;&#x5316;&#x7684;&#x6982;&#x5FF5;">2.&#x7EC4;&#x4EF6;&#x5316;&#x7684;&#x6982;&#x5FF5; <a href="#t22.&#x7EC4;&#x4EF6;&#x5316;&#x7684;&#x6982;&#x5FF5;"> # </a></h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5F88;&#x76F4;&#x89C2;&#x7684;&#x5C06;&#x4E00;&#x4E2A;&#x590D;&#x6742;&#x7684;&#x9875;&#x9762;&#x5206;&#x5272;&#x6210;&#x82E5;&#x5E72;&#x4E2A;&#x72EC;&#x7ACB;&#x7EC4;&#x4EF6;,&#x6BCF;&#x4E2A;&#x7EC4;&#x4EF6;&#x5305;&#x542B;&#x81EA;&#x5DF1;&#x7684;&#x903B;&#x8F91;&#x548C;&#x6837;&#x5F0F; &#x518D;&#x5C06;&#x8FD9;&#x4E9B;&#x72EC;&#x7ACB;&#x7EC4;&#x4EF6;&#x7EC4;&#x5408;&#x5B8C;&#x6210;&#x4E00;&#x4E2A;&#x590D;&#x6742;&#x7684;&#x9875;&#x9762;&#x3002; &#x8FD9;&#x6837;&#x65E2;&#x51CF;&#x5C11;&#x4E86;&#x903B;&#x8F91;&#x590D;&#x6742;&#x5EA6;&#xFF0C;&#x53C8;&#x5B9E;&#x73B0;&#x4E86;&#x4EE3;&#x7801;&#x7684;&#x91CD;&#x7528;</p>
<ul>
<li>&#x53EF;&#x7EC4;&#x5408;&#xFF1A;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x548C;&#x5176;&#x4ED6;&#x7684;&#x7EC4;&#x4EF6;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#x6216;&#x8005;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5D4C;&#x5957;&#x5728;&#x53E6;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x5185;&#x90E8;</li>
<li>&#x53EF;&#x91CD;&#x7528;&#xFF1A;&#x6BCF;&#x4E2A;&#x7EC4;&#x4EF6;&#x90FD;&#x662F;&#x5177;&#x6709;&#x72EC;&#x7ACB;&#x529F;&#x80FD;&#x7684;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x88AB;&#x4F7F;&#x7528;&#x5728;&#x591A;&#x4E2A;&#x573A;&#x666F;&#x4E2D;</li>
<li>&#x53EF;&#x7EF4;&#x62A4;&#xFF1A;&#x6BCF;&#x4E2A;&#x5C0F;&#x7684;&#x7EC4;&#x4EF6;&#x4EC5;&#x4EC5;&#x5305;&#x542B;&#x81EA;&#x8EAB;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x66F4;&#x5BB9;&#x6613;&#x88AB;&#x7406;&#x89E3;&#x548C;&#x7EF4;&#x62A4;</li>
</ul>
<p><a href="https://pan.baidu.com/s/1hsivfN2">&#x7EC4;&#x4EF6;&#x5316;</a></p>
<h2 id="t33.&#x8DD1;&#x901A;react&#x5F00;&#x53D1;&#x73AF;&#x5883;">3.&#x8DD1;&#x901A;react&#x5F00;&#x53D1;&#x73AF;&#x5883; <a href="#t33.&#x8DD1;&#x901A;react&#x5F00;&#x53D1;&#x73AF;&#x5883;"> # </a></h2>
<pre><code class="lang-js">$ npm install create-react-app -g
$ create-react-app &lt;project-name&gt;
$ cd &lt;project-name&gt; &amp;&amp; npm start
</code></pre>
<p>&#x9ED8;&#x8BA4;&#x4F1A;&#x81EA;&#x52A8;&#x5B89;&#x88C5;React,react&#x7531;&#x4E24;&#x90E8;&#x5206;&#x7EC4;&#x6210;,&#x5206;&#x522B;&#x662F;:</p>
<ul>
<li>react.js &#x662F; React &#x7684;&#x6838;&#x5FC3;&#x5E93;</li>
<li>react-dom.js &#x662F;&#x63D0;&#x4F9B;&#x4E0E;DOM&#x76F8;&#x5173;&#x7684;&#x529F;&#x80FD;,&#x4F1A;&#x5728;window&#x4E0B;&#x589E;&#x52A0;ReactDOM&#x5C5E;&#x6027;,&#x5185;&#x90E8;&#x6BD4;&#x8F83;&#x91CD;&#x8981;&#x7684;&#x65B9;&#x6CD5;&#x662F;render,&#x5C06;react&#x5143;&#x7D20;&#x6216;&#x8005;react&#x7EC4;&#x4EF6;&#x63D2;&#x5165;&#x5230;&#x9875;&#x9762;&#x4E2D;&#x3002;</li>
</ul>
<h2 id="t44.&#x7B80;&#x4ECB;JSX">4.&#x7B80;&#x4ECB;JSX <a href="#t44.&#x7B80;&#x4ECB;JSX"> # </a></h2>
<p>&#x662F;&#x4E00;&#x79CD;JS&#x548C;HTML&#x6DF7;&#x5408;&#x7684;&#x8BED;&#x6CD5;,&#x5C06;&#x7EC4;&#x4EF6;&#x7684;&#x7ED3;&#x6784;&#x3001;&#x6570;&#x636E;&#x751A;&#x81F3;&#x6837;&#x5F0F;&#x90FD;&#x805A;&#x5408;&#x5728;&#x4E00;&#x8D77;&#x5B9A;&#x4E49;&#x7EC4;&#x4EF6;,&#x4F1A;&#x7F16;&#x8BD1;&#x6210;&#x666E;&#x901A;&#x7684;Javascript&#x3002;
&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;JSX&#x5E76;&#x4E0D;&#x662F;html,&#x5728;JSX&#x4E2D;&#x5C5E;&#x6027;&#x4E0D;&#x80FD;&#x5305;&#x542B;&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x50CF;class&#x9700;&#x8981;&#x5199;&#x6210;className,for&#x9700;&#x8981;&#x5199;&#x6210;htmlFor,&#x5E76;&#x4E14;&#x5C5E;&#x6027;&#x540D;&#x9700;&#x8981;&#x91C7;&#x7528;&#x9A7C;&#x5CF0;&#x547D;&#x540D;&#x6CD5;&#xFF01;</p>
<h2 id="t55.createElement">5.createElement <a href="#t55.createElement"> # </a></h2>
<p>JSX&#x5176;&#x5B9E;&#x53EA;&#x662F;&#x4E00;&#x79CD;&#x8BED;&#x6CD5;&#x7CD6;,&#x6700;&#x7EC8;&#x4F1A;&#x901A;&#x8FC7;babel&#x8F6C;&#x8BD1;&#x6210;createElement&#x8BED;&#x6CD5;,&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x7B49;&#x4EF7;</p>
<pre><code class="lang-js">ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>&#x59DC;,<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>&#x5E05;&#x54E5;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>);
ReactDOM.render(React.createElement(<span class="hljs-string">&quot;div&quot;</span>,<span class="hljs-literal">null</span>,<span class="hljs-string">&quot;&#x59DC;,&quot;</span>,React.createElement(<span class="hljs-string">&quot;span&quot;</span>,<span class="hljs-literal">null</span>,<span class="hljs-string">&quot;&#x5E05;&#x54E5;&quot;</span>)));
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E00;&#x822C;&#x4F7F;&#x7528;React.createElement&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x865A;&#x62DF;dom&#x5143;&#x7D20;&#x3002;</p>
<h2 id="t66.react&#x5143;&#x7D20;/JSX&#x5143;&#x7D20;">6.react&#x5143;&#x7D20;/JSX&#x5143;&#x7D20; <a href="#t66.react&#x5143;&#x7D20;/JSX&#x5143;&#x7D20;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ReactElement</span>(<span class="hljs-params">type,props</span>) </span>{
    <span class="hljs-keyword">this</span>.type = type;
    <span class="hljs-keyword">this</span>.props = props;
}
<span class="hljs-keyword">let</span> React = {
    createElement(type,props={},...childrens){
        childrens.length===<span class="hljs-number">1</span>?childrens = childrens[<span class="hljs-number">0</span>]:<span class="hljs-keyword">void</span> <span class="hljs-number">0</span>
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ReactElement(type,{...props,<span class="hljs-attr">children</span>:childrens})
    }
};
</code></pre>
<p>ReactElement&#x5C31;&#x662F;&#x865A;&#x62DF;dom&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x5177;&#x6709;&#x4E00;&#x4E2A;type&#x5C5E;&#x6027;&#x4EE3;&#x8868;&#x5F53;&#x524D;&#x7684;&#x8282;&#x70B9;&#x7C7B;&#x578B;&#xFF0C;&#x8FD8;&#x6709;&#x8282;&#x70B9;&#x7684;&#x5C5E;&#x6027;props</p>
<h2 id="t77.&#x6A21;&#x62DF;render&#x5B9E;&#x73B0;">7.&#x6A21;&#x62DF;render&#x5B9E;&#x73B0; <a href="#t77.&#x6A21;&#x62DF;render&#x5B9E;&#x73B0;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> render = <span class="hljs-function">(<span class="hljs-params">eleObj,container</span>)=&gt;</span>{
    <span class="hljs-comment">// &#x5148;&#x53D6;&#x51FA;&#x7B2C;&#x4E00;&#x5C42; &#x8FDB;&#x884C;&#x521B;&#x5EFA;&#x771F;&#x5B9E;dom</span>
    <span class="hljs-keyword">let</span> {type,props} = eleObj;
    <span class="hljs-keyword">let</span> elementNode = <span class="hljs-built_in">document</span>.createElement(type); <span class="hljs-comment">// &#x521B;&#x5EFA;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;</span>
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> attr <span class="hljs-keyword">in</span> props){ <span class="hljs-comment">// &#x5FAA;&#x73AF;&#x6240;&#x6709;&#x5C5E;&#x6027;</span>
        <span class="hljs-keyword">if</span>(attr === <span class="hljs-string">&apos;children&apos;</span>){ <span class="hljs-comment">// &#x5982;&#x679C;&#x662F;children&#x8868;&#x793A;&#x6709;&#x5D4C;&#x5957;&#x5173;&#x7CFB;</span>
            <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> props[attr] == <span class="hljs-string">&apos;object&apos;</span>){ <span class="hljs-comment">// &#x770B;&#x662F;&#x5426;&#x662F;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6587;&#x672C;&#x8282;&#x70B9;</span>
                props[attr].forEach(<span class="hljs-function"><span class="hljs-params">item</span>=&gt;</span>{ <span class="hljs-comment">// &#x591A;&#x4E2A;&#x7684;&#x8BDD;&#x5FAA;&#x73AF;&#x5224;&#x65AD; &#x5982;&#x679C;&#x662F;&#x5BF9;&#x8C61;&#x518D;&#x6B21;&#x8C03;&#x7528;render&#x65B9;&#x6CD5;</span>
                    <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> item === <span class="hljs-string">&apos;object&apos;</span>){
                        render(item,elementNode)
                    }<span class="hljs-keyword">else</span>{ <span class="hljs-comment">//&#x662F;&#x6587;&#x672C;&#x8282;&#x70B9; &#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x5373;&#x53EF;</span>
                        elementNode.appendChild(<span class="hljs-built_in">document</span>.createTextNode(item));
                    }
                })
            }<span class="hljs-keyword">else</span>{ <span class="hljs-comment">// &#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6587;&#x672C;&#x8282;&#x70B9;&#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x5373;&#x53EF;</span>
                elementNode.appendChild(<span class="hljs-built_in">document</span>.createTextNode(props[attr]));
            }
        }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(attr === <span class="hljs-string">&apos;className&apos;</span>){ <span class="hljs-comment">// &#x662F;&#x4E0D;&#x662F;class&#x5C5E;&#x6027; class &#x5C5E;&#x6027;&#x7279;&#x6B8A;&#x5904;&#x7406;</span>
            elementNode.setAttribute(<span class="hljs-string">&apos;class&apos;</span>,props[attr]);
        }<span class="hljs-keyword">else</span>{
            elementNode.setAttribute(attr,props[attr]);
        }
    }
    container.appendChild(elementNode)
};
</code></pre>
<h2 id="t88.JSX&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7528;&#x6CD5;">8.JSX&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7528;&#x6CD5; <a href="#t88.JSX&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7528;&#x6CD5;"> # </a></h2>
<p>1) &#x53EF;&#x4EE5;&#x653E;JS&#x7684;&#x6267;&#x884C;&#x7ED3;&#x679C;
2) &#x5982;&#x679C;&#x6362;&#x884C;&#x9700;&#x8981;&#x7528;()&#x5305;&#x88F9;jsx&#x4EE3;&#x7801;
3) &#x53EF;&#x4EE5;&#x628A;JSX&#x5143;&#x7D20;&#x5F53;&#x4F5C;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;
4) &lt;{&#x6765;&#x5224;&#x65AD;&#x662F;&#x8868;&#x8FBE;&#x5F0F;&#x8FD8;&#x662F;js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toResult</span>(<span class="hljs-params">{name,age}</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>&#x4ECA;&#x5E74;{name},{age}&#x5C81;&#x4E86;!<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
}
<span class="hljs-keyword">let</span> arrs =  [{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">8</span>},,{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;&#x59DC;&#x6587;&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">28</span>}];
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    {arrs.map(((item,index)=&gt;(
        typeof item===&apos;object&apos;?<span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>&gt;</span>{toResult(item)}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>:null
    )))}
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>,<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;root&apos;</span>));
</code></pre>
<p>null&#x4E5F;&#x662F;&#x5408;&#x6CD5;&#x5143;&#x7D20;,&#x5FAA;&#x73AF;&#x65F6;&#x9700;&#x8981;&#x5E26;key&#x5C5E;&#x6027;</p>
<h2 id="t99.JSX&#x5C5E;&#x6027;">9.JSX&#x5C5E;&#x6027; <a href="#t99.JSX&#x5C5E;&#x6027;"> # </a></h2>
<p>&#x5728;JSX&#x4E2D;&#x5206;&#x4E3A;&#x666E;&#x901A;&#x5C5E;&#x6027;&#x548C;&#x7279;&#x6B8A;&#x5C5E;&#x6027;&#xFF0C;&#x50CF;class&#x8981;&#x5199;&#x6210;className,for&#x8981;&#x5199;&#x6210;htmlFor
style&#x8981;&#x91C7;&#x7528;&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x5F0F;
dangerouslyInnerHTML&#x63D2;&#x5165;html</p>
<h2 id="t1010.&#x7EC4;&#x4EF6;&#x7684;&#x7279;&#x70B9;&#x58F0;&#x660E;&#x65B9;&#x5F0F;">10.&#x7EC4;&#x4EF6;&#x7684;&#x7279;&#x70B9;&#x58F0;&#x660E;&#x65B9;&#x5F0F; <a href="#t1010.&#x7EC4;&#x4EF6;&#x7684;&#x7279;&#x70B9;&#x58F0;&#x660E;&#x65B9;&#x5F0F;"> # </a></h2>
<p>react&#x5143;&#x7D20;&#x662F;&#x662F;&#x7EC4;&#x4EF6;&#x7EC4;&#x6210;&#x7684;&#x57FA;&#x672C;&#x5355;&#x4F4D;</p>
<p>&#x9996;&#x5B57;&#x6BCD;&#x5FC5;&#x987B;&#x5927;&#x5199;,&#x76EE;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x548C;JSX&#x5143;&#x7D20;&#x8FDB;&#x884C;&#x533A;&#x5206;
&#x7EC4;&#x4EF6;&#x5B9A;&#x4E49;&#x540E;&#x53EF;&#x4EE5;&#x50CF;JSX&#x5143;&#x7D20;&#x4E00;&#x6837;&#x8FDB;&#x884C;&#x4F7F;&#x7528;
&#x6BCF;&#x4E2A;&#x7EC4;&#x4EF6;&#x5FC5;&#x987B;&#x8FD4;&#x56DE;&#x552F;&#x4E00;&#x7684;&#x9876;&#x7EA7;JSX&#x5143;&#x7D20;
&#x53EF;&#x4EE5;&#x901A;&#x8FC7;render&#x65B9;&#x6CD5;&#x5C06;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x6210;&#x771F;&#x5B9E;DOM</p>
<h2 id="t1111.&#x7EC4;&#x4EF6;&#x7684;&#x4E24;&#x79CD;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;">11.&#x7EC4;&#x4EF6;&#x7684;&#x4E24;&#x79CD;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F; <a href="#t1111.&#x7EC4;&#x4EF6;&#x7684;&#x4E24;&#x79CD;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;"> # </a></h2>
<p>react&#x600E;&#x4E48;&#x533A;&#x5206;&#x662F;&#x7EC4;&#x4EF6;&#x8FD8;&#x662F;jsx&#x5143;&#x7D20;&#xFF1F;&#x7EC4;&#x4EF6;&#x540D;&#x9700;&#x8981;&#x5F00;&#x5934;&#x5927;&#x5199;&#xFF0C;react&#x7EC4;&#x4EF6;&#x5F53;&#x4F5C;jsx&#x6765;&#x8FDB;&#x884C;&#x4F7F;&#x7528;</p>
<p>&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x51FD;&#x6570;&#x58F0;&#x660E;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Build</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{props.name} {props.age}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
}
render(&lt;div&gt;
  &lt;Build name={school1.name} age={school1.age}/&gt;
  &lt;Build {...school2} /&gt;
&lt;/div&gt;,window.root);
</code></pre>
<p>&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x7C7B;&#x58F0;&#x660E;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Build</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
  render(){
      <span class="hljs-keyword">let</span> {name,age} = <span class="hljs-keyword">this</span>.props;
      <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{name} {age}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
  }
}
</code></pre>
<p>&#x7C7B;&#x58F0;&#x660E;&#x6709;&#x72B6;&#x6001;&#xFF0C;this&#xFF0C;&#x548C;&#x58F0;&#x660E;&#x5468;&#x671F;</p>
<h2 id="t1212.&#x7EC4;&#x4EF6;&#x4E2D;&#x5C5E;&#x6027;&#x548C;&#x72B6;&#x6001;&#x7684;&#x533A;&#x522B;">12.&#x7EC4;&#x4EF6;&#x4E2D;&#x5C5E;&#x6027;&#x548C;&#x72B6;&#x6001;&#x7684;&#x533A;&#x522B; <a href="#t1212.&#x7EC4;&#x4EF6;&#x4E2D;&#x5C5E;&#x6027;&#x548C;&#x72B6;&#x6001;&#x7684;&#x533A;&#x522B;"> # </a></h2>
<p>&#x7EC4;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x6765;&#x6E90;&#x6709;&#x4E24;&#x4E2A;&#x5730;&#x65B9;
props &#x5916;&#x754C;&#x4F20;&#x9012;&#x8FC7;&#x6765;&#x7684;(&#x9ED8;&#x8BA4;&#x5C5E;&#x6027;&#xFF0C;&#x5C5E;&#x6027;&#x6821;&#x9A8C;)
state &#x72B6;&#x6001;&#x662F;&#x81EA;&#x5DF1;&#x7684;,&#x6539;&#x53D8;&#x72B6;&#x6001;&#x552F;&#x4E00;&#x7684;&#x65B9;&#x5F0F;&#x5C31;&#x662F;setState
&#x5C5E;&#x6027;&#x548C;&#x72B6;&#x6001;&#x7684;&#x53D8;&#x5316;&#x90FD;&#x4F1A;&#x5F71;&#x54CD;&#x89C6;&#x56FE;&#x66F4;&#x65B0;</p>
<h2 id="t1313.&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6;">13.&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6; <a href="#t1313.&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6;"> # </a></h2>
<p>&#x7ED9;&#x5143;&#x7D20;&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6;&#xFF0C;&#x4E8B;&#x4EF6;&#x7ED1;&#x5B9A;&#x65B9;&#x5F0F;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Clock</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    <span class="hljs-keyword">constructor</span>(){
        <span class="hljs-keyword">super</span>();
        <span class="hljs-keyword">this</span>.state = {<span class="hljs-attr">date</span>:<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toLocaleString()}
    }
    componentDidMount(){ <span class="hljs-comment">//&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x5B8C;&#x6210;&#xFF0C;&#x5F53;&#x6E32;&#x67D3;&#x540E;&#x4F1A;&#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x6B64;&#x51FD;&#x6570;</span>
        <span class="hljs-keyword">this</span>.timer = setInterval(<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{ <span class="hljs-comment">// &#x7BAD;&#x5934;&#x51FD;&#x6570; &#x5426;&#x5219;this &#x6307;&#x5411;&#x7684;&#x662F;window</span>
            <span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">date</span>:<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toLocaleString()})
        },<span class="hljs-number">1000</span>);
    }
    componentWillUnmount(){ <span class="hljs-comment">//&#x7EC4;&#x4EF6;&#x5C06;&#x8981;&#x5378;&#x8F7D;&#xFF0C;&#x5F53;&#x7EC4;&#x4EF6;&#x79FB;&#x9664;&#x65F6;&#x4F1A;&#x8C03;&#x7528;</span>
        clearInterval(<span class="hljs-keyword">this</span>.timer); <span class="hljs-comment">//&#x4E00;&#x822C;&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4E2D; &#x6E05;&#x9664;&#x5B9A;&#x65F6;&#x5668;&#x548C;&#x7ED1;&#x5B9A;&#x7684;&#x4E8B;&#x4EF6;</span>
    }
    destroy=<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{ <span class="hljs-comment">//es7 &#x7BAD;&#x5934;&#x51FD;&#x6570;</span>
        <span class="hljs-comment">// &#x5220;&#x9664;&#x67D0;&#x4E2A;&#x7EC4;&#x4EF6;</span>
        ReactDOM.unmountComponentAtNode(<span class="hljs-built_in">window</span>.root);
    }
    render(){
        <span class="hljs-comment">// &#x7ED9;react&#x5143;&#x7D20;&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6;&#x9ED8;&#x8BA4;this&#x662F;undefined,bind&#x65B9;&#x5F0F; &#x5728;&#x5C31;&#x662F;&#x7BAD;&#x5934;&#x51FD;&#x6570;</span>
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.destroy}</span>&gt;</span>{this.state.date}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
    }
}
</code></pre>
<p>// &#x6267;&#x884C;&#x987A;&#x5E8F; constructor -&gt; render -&gt; componentDidMount -&gt; setState-&gt; render - onClick-&gt; unmountComponentAtNode -&gt; componentWillUnmount -&gt; clearInterval
ReactDOM.render(<clock>,window.root);
&#x7ED9;jsx&#x5143;&#x7D20;&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6;&#x8981;&#x6CE8;&#x610F;&#x4E8B;&#x4EF6;&#x4E2D;&#x7684;this&#x6307;&#x5411;&#xFF0C;&#x4E8B;&#x4EF6;&#x540D;&#x91C7;&#x7528; on+&quot;&#x5F00;&#x5934;&#x5927;&#x5199;&#x4E8B;&#x4EF6;&#x540D;&quot;&#x7684;&#x65B9;&#x5F0F;</clock></p>
<h2 id="t1414.&#x5C5E;&#x6027;&#x6821;&#x9A8C;,&#x9ED8;&#x8BA4;&#x5C5E;&#x6027;">14.&#x5C5E;&#x6027;&#x6821;&#x9A8C;,&#x9ED8;&#x8BA4;&#x5C5E;&#x6027; <a href="#t1414.&#x5C5E;&#x6027;&#x6821;&#x9A8C;,&#x9ED8;&#x8BA4;&#x5C5E;&#x6027;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;prop-types&apos;</span>; <span class="hljs-comment">//&#x5F15;&#x5165;&#x5C5E;&#x6027;&#x6821;&#x9A8C;&#x7684;&#x6A21;&#x5757;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">School</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span></span>{ <span class="hljs-comment">// &#x7C7B;&#x4E0A;&#x7684;&#x5C5E;&#x6027;&#x5C31;&#x53EB;&#x9759;&#x6001;&#x5C5E;&#x6027;</span>
    <span class="hljs-keyword">static</span> propTypes = { <span class="hljs-comment">// &#x6821;&#x9A8C;&#x5C5E;&#x6027;&#x7684;&#x7C7B;&#x578B;&#x548C;&#x662F;&#x5426;&#x5FC5;&#x586B;</span>
        age:PropTypes.number.isRequired, <span class="hljs-comment">// &#x652F;&#x6301;&#x7684;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x53C2;&#x8003;prop-types&#x7684;readme&#x6587;&#x4EF6;</span>
    };
    <span class="hljs-keyword">static</span> defaultProps = { <span class="hljs-comment">// &#x5148;&#x9ED8;&#x8BA4;&#x8C03;&#x7528;defaultProps</span>
        name:<span class="hljs-string">&apos;&#x73E0;&#x5CF0;&apos;</span>,
        <span class="hljs-attr">age</span>:<span class="hljs-number">1</span>
    }; <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x5C5E;&#x6027;</span>
    <span class="hljs-keyword">constructor</span>(props){ <span class="hljs-comment">//&#x5982;&#x679C;&#x60F3;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x62FF;&#x5230;&#x5C5E;&#x6027;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x53C2;&#x6570;&#x7684;&#x65B9;&#x5F0F;</span>
         <span class="hljs-comment">//&#x4E0D;&#x80FD;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x66F4;&#x6539;&#x5C5E;&#x6027; &#x4E0D;&#x80FD;&#x4FEE;&#x6539;&#x5C5E;&#x6027;*</span>
        <span class="hljs-keyword">super</span>();
    }
    render(){
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{this.props.name} {this.props.age}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
    }
}
propTypes&#x548C;defaultProps&#x540D;&#x5B57;&#x4E0D;&#x80FD;&#x66F4;&#x6539;&#xFF0C;&#x8FD9;&#x662F;react&#x89C4;&#x5B9A;&#x597D;&#x7684;&#x540D;&#x79F0;
</code></pre>
<h2 id="t1515.&#x72B6;&#x6001;&#x7684;&#x4F7F;&#x7528;">15.&#x72B6;&#x6001;&#x7684;&#x4F7F;&#x7528; <a href="#t1515.&#x72B6;&#x6001;&#x7684;&#x4F7F;&#x7528;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">constructor</span>(){
        <span class="hljs-keyword">super</span>();
        <span class="hljs-keyword">this</span>.state = {<span class="hljs-attr">count</span>:<span class="hljs-number">0</span>}
    };
    handleClick = <span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{
        <span class="hljs-comment">// setState&#x65B9;&#x6CD5;&#x4F1A;&#x8FDB;&#x884C;&#x5408;&#x5E76; setState&#x6709;&#x4E24;&#x79CD;&#x5199;&#x6CD5; &#x4E00;&#x79CD;&#x662F;&#x5BF9;&#x8C61;&#x4E00;&#x79CD;&#x662F;&#x51FD;&#x6570;</span>
        <span class="hljs-comment">/*this.setState({count:this.state.count+1});
          this.setState({count:this.state.count+1});*/</span>
        <span class="hljs-comment">//this.setState((prevState)=&gt;({count:prevState.count+1})); //&#x5982;&#x679C;&#x8FD4;&#x56DE;&#x7684;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x7528;&#x5C0F;&#x62EC;&#x53F7;&#x5305;&#x88F9;</span>
        <span class="hljs-comment">//this.setState((prevState)=&gt;({count:prevState.count+1}));</span>
        <span class="hljs-comment">// &#x4E0B;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x662F;&#x4F9D;&#x8D56;&#x4E8E;&#x4E0A;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x65F6;&#x9700;&#x8981;&#x5199;&#x6210;&#x51FD;&#x6570;&#x7684;&#x65B9;&#x5F0F;</span>
        <span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">count</span>:<span class="hljs-keyword">this</span>.state.count+<span class="hljs-number">1</span>},<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">count</span>:<span class="hljs-keyword">this</span>.state.count+<span class="hljs-number">1</span>});
        }); <span class="hljs-comment">// &#x8FD9;&#x4E2A;&#x5199;&#x6CD5;&#x7B49;&#x540C;&#x4E8E; this.setState((prevState)=&gt;({count:prevState.count+1}));</span>
    };
    render(){
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;render&apos;</span>);
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
                {this.state.count}
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleClick}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
        )
    }
}
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Counter</span>/&gt;</span></span>,<span class="hljs-built_in">window</span>.root);
</code></pre>
<p>&#x5982;&#x679C;&#x8BBE;&#x7F6E;&#x591A;&#x4E2A;&#x72B6;&#x6001;setState&#x4F1A;&#x5408;&#x5E76;&#xFF0C;&#x5982;&#x679C;&#x4E0B;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x4F9D;&#x8D56;&#x4E8E;&#x4E0A;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#xFF0C;&#x9700;&#x8981;&#x5199;&#x6210;&#x51FD;&#x6570;&#x7684;&#x65B9;&#x5F0F;</p>
<h2 id="t1616.&#x590D;&#x5408;&#x7EC4;&#x4EF6;">16.&#x590D;&#x5408;&#x7EC4;&#x4EF6; <a href="#t1616.&#x590D;&#x5408;&#x7EC4;&#x4EF6;"> # </a></h2>
<p>&#x590D;&#x5408;&#x7EC4;&#x4EF6;&#x5C31;&#x662F;&#x5C06;&#x591A;&#x4E2A;&#x7EC4;&#x4EF6;&#x8FDB;&#x884C;&#x7EC4;&#x5408;&#xFF0C;&#x7ED3;&#x6784;&#x975E;&#x5E38;&#x590D;&#x6742;&#x65F6;&#x53EF;&#x4EE5;&#x628A;&#x7EC4;&#x4EF6;&#x5206;&#x79BB;&#x5F00;</p>
<p>&#x7236;&#x5B50;&#x7EC4;&#x4EF6;&#x7684;&#x901A;&#x4FE1;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Panel</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render(){
        <span class="hljs-keyword">let</span> {header,body} = <span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            &lt;div className=&quot;container&quot;&gt;
                &lt;div className=&quot;panel-default panel&quot;&gt;
                    &lt;Header head={header}&gt;&lt;/Header&gt;
                    &lt;Body b={body}/&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        )
    }
} // react&#x79CD;&#x9700;&#x8981;&#x5C06;&#x5C5E;&#x6027;&#x4E00;&#x5C42;&#x5C42;&#x5411;&#x4E0B;&#x4F20;&#x9012; &#x5355;&#x5411;&#x6570;&#x636E;&#x6D41;
class Body extends Component{
    render(){return (&lt;div className=&quot;panel-body&quot;&gt;{this.props.b}&lt;/div&gt;)}
}
class Header extends Component{
    render(){return (&lt;div className=&quot;panel-heading&quot;&gt;{this.props.head}&lt;/div&gt;)}
}
let data = {header:&apos;&#x6211;&#x975E;&#x5E38;&#x5E05;&apos;,body:&apos;&#x957F;&#x7684;&#x5E05;&apos;};
ReactDOM.render(&lt;Panel {...data}/&gt;,window.root);
</code></pre>
<p>&#x5B50;&#x7236;&#x7EC4;&#x4EF6;&#x7684;&#x901A;&#x4FE1;
&#x901A;&#x8FC7;&#x7236;&#x4EB2;&#x4F20;&#x9012;&#x7ED9;&#x513F;&#x5B50;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x513F;&#x5B50;&#x8C03;&#x7528;&#x7236;&#x4EB2;&#x7684;&#x51FD;&#x6570;&#x5C06;&#x503C;&#x4F20;&#x9012;&#x7ED9;&#x7236;&#x4EB2;,&#x7236;&#x4EB2;&#x66F4;&#x65B0;&#x503C;&#xFF0C;&#x5237;&#x65B0;&#x89C6;&#x56FE;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Panel</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">constructor</span>(){
        <span class="hljs-keyword">super</span>();
        <span class="hljs-keyword">this</span>.state = {<span class="hljs-attr">color</span>:<span class="hljs-string">&apos;primary&apos;</span>}
    }
    changeColor=<span class="hljs-function">(<span class="hljs-params">color</span>)=&gt;</span>{ <span class="hljs-comment">//&#x5230;&#x65F6;&#x5019;&#x513F;&#x5B50;&#x4F20;&#x9012;&#x4E00;&#x4E2A;&#x989C;&#x8272;</span>
        <span class="hljs-keyword">this</span>.setState({color});
    };
    render(){
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>&quot;<span class="hljs-attr">panel-</span>&quot;+<span class="hljs-attr">this.state.color</span>+&quot; <span class="hljs-attr">panel</span>&quot;}&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Header</span> <span class="hljs-attr">head</span>=<span class="hljs-string">{this.props.header}</span>
                            <span class="hljs-attr">change</span>=<span class="hljs-string">{this.changeColor}</span>
                    &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Header</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Header</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    handleClick = <span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{
        <span class="hljs-keyword">this</span>.props.change(<span class="hljs-string">&apos;danger&apos;</span>); <span class="hljs-comment">//&#x8C03;&#x7528;&#x7236;&#x4EB2;&#x7684;&#x65B9;&#x6CD5;</span>
    };
    render(){<span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;panel-heading&quot;</span>&gt;</span>
        {this.props.head} <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;btn btn-danger&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleClick}</span>&gt;</span>&#x6539;&#x989C;&#x8272;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>)}
}
</code></pre>
<h2 id="t1717.&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;&#x548C;&#x975E;&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;">17.&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;&#x548C;&#x975E;&#x53D7;&#x63A7;&#x7EC4;&#x4EF6; <a href="#t1717.&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;&#x548C;&#x975E;&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;"> # </a></h2>
<p>&#x53D7;&#x72B6;&#x6001;&#x63A7;&#x5236;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x6709;onChange&#x65B9;&#x6CD5;&#xFF0C;&#x5426;&#x5219;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;
&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x8D4B;&#x4E88;&#x9ED8;&#x8BA4;&#x503C;&#xFF08;&#x5B98;&#x65B9;&#x63A8;&#x8350;&#x4F7F;&#x7528; &#x53D7;&#x63A7;&#x7EC4;&#x4EF6;&#xFF09;
&#x5B9E;&#x73B0;&#x53CC;&#x5411;&#x6570;&#x636E;&#x7ED1;&#x5B9A;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Input</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">constructor</span>(){
        <span class="hljs-keyword">super</span>();
        <span class="hljs-keyword">this</span>.state = {<span class="hljs-attr">val</span>:<span class="hljs-string">&apos;100&apos;</span>}
    }
    handleChange = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span>{ <span class="hljs-comment">//e&#x662F;&#x4E8B;&#x4EF6;&#x6E90;</span>
        <span class="hljs-keyword">let</span> val = e.target.value;
        <span class="hljs-keyword">this</span>.setState({val});
    };
    render(){
        <span class="hljs-keyword">return</span> (<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.val}</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}/</span>&gt;</span>
            {this.state.val}
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>)
    }
}
</span></code></pre>
<p>&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sum</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">constructor</span>(){
        <span class="hljs-keyword">super</span>();
        <span class="hljs-keyword">this</span>.state = {<span class="hljs-attr">a</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">b</span>:<span class="hljs-number">1</span>}
    }
    <span class="hljs-comment">// key&#x8868;&#x793A;&#x7684;&#x5C31;&#x662F;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x6539;&#x7684;&#x662F;&#x54EA;&#x4E00;&#x4E2A;</span>
    <span class="hljs-comment">// e&#x8868;&#x793A;&#x7684;&#x662F;&#x4E8B;&#x4EF6;&#x6E90;</span>
    handleChange(key,e){ <span class="hljs-comment">//&#x5904;&#x7406;&#x591A;&#x4E2A;&#x8F93;&#x5165;&#x6846;&#x7684;&#x503C;&#x6620;&#x5C04;&#x5230;&#x72B6;&#x6001;&#x7684;&#x65B9;&#x6CD5;</span>
        <span class="hljs-keyword">this</span>.setState({
            [key]:<span class="hljs-built_in">parseInt</span>(e.target.value)||<span class="hljs-number">0</span>
        })
    }
    render(){
        <span class="hljs-keyword">return</span> (
            &lt;div&gt;
                &lt;input type=&quot;number&quot; value={this.state.a} onChange={e=&gt;{this.handleChange(&apos;a&apos;,e)}}/&gt;
                &lt;input type=&quot;number&quot; value={this.state.b} onChange={e=&gt;{this.handleChange(&apos;b&apos;,e)}}/&gt;
                {this.state.a+this.state.b}
            &lt;/div&gt;
        )
    }
}
</code></pre>
<p>&#x975E;&#x53D7;&#x63A7;&#x7EC4;&#x4EF6;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sum</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">constructor</span>(){
        <span class="hljs-keyword">super</span>();
        <span class="hljs-keyword">this</span>.state =  {<span class="hljs-attr">result</span>:<span class="hljs-string">&apos;&apos;</span>}
    }
    <span class="hljs-comment">//&#x901A;&#x8FC7;ref&#x8BBE;&#x7F6E;&#x7684;&#x5C5E;&#x6027; &#x53EF;&#x4EE5;&#x901A;&#x8FC7;this.refs&#x83B7;&#x53D6;&#x5230;&#x5BF9;&#x5E94;&#x7684;dom&#x5143;&#x7D20;</span>
    handleChange = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span>{
        <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">this</span>.refs.a.value + <span class="hljs-keyword">this</span>.b.value;
        <span class="hljs-keyword">this</span>.setState({result});
    };
    render(){
        <span class="hljs-keyword">return</span> (
            &lt;div onChange={this.handleChange}&gt;
                &lt;input type=&quot;number&quot; ref=&quot;a&quot;/&gt;
                {/*x&#x4EE3;&#x8868;&#x7684;&#x771F;&#x5B9E;&#x7684;dom,&#x628A;&#x5143;&#x7D20;&#x6302;&#x8F7D;&#x5728;&#x4E86;&#x5F53;&#x524D;&#x5B9E;&#x4F8B;&#x4E0A;*/}
                &lt;input type=&quot;number&quot; ref={(x)=&gt;{
                    this.b = x;
                }}/&gt;
                {this.state.result}
            &lt;/div&gt;
        )
    }
}
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
